﻿#include "ByteArrayInputStream.h"
#include "array/Array.hpp"
using namespace yzrilyzr_array;

namespace yzrilyzr_io{
	ByteArrayInputStream::ByteArrayInputStream(const ByteArray& s){
		this->buf=s;
		this->pos=0;
	}
	int32_t ByteArrayInputStream::read(){
		return pos < buf.length?buf[pos++] & 0xff:-1;
	}
	u_index ByteArrayInputStream::read(ByteArray & b, u_index off, u_index len){
		if(pos >= buf.length){
			return -1;
		} else{
			u_index avail=buf.length - pos;
			if(len > avail){
				len=avail;
			}
			if(len <= 0){
				return 0;
			} else{
				for(u_index i=0;i < len;i++){
					b[off + i]=buf[pos + i];
				}
				pos+=len;
				return len;
			}
		}
	}
	u_index ByteArrayInputStream::available(){
		return buf.length - pos;
	}
	void ByteArrayInputStream::close(){}
}